package edu.ycxy.poetry.service.serviceImpl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.ZhiShi;
import edu.ycxy.poetry.mapper.ZhiShiMapper;
import edu.ycxy.poetry.service.IZhiShiService;
import edu.ycxy.poetry.vo.AddKnowVo;
import edu.ycxy.poetry.vo.UpdateKnowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@Service
public class ZhiShiServiceImpl extends ServiceImpl<ZhiShiMapper, ZhiShi> implements IZhiShiService {

    @Autowired
    private ZhiShiMapper zhiShiMapper;

    //根据id倒序查询知识表中的所有内容
    @Override
    public PageInfo<ZhiShi> selectList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ZhiShi> zhiShis = zhiShiMapper.selectList();
        PageInfo pageInfo = new PageInfo(zhiShis);
        return pageInfo;
    }

    //根据知识的标题查询知识的详细信息
    @Override
    public ZhiShi selectByTitle(String title) {
        return zhiShiMapper.selectByTitle(title);
    }

    /**
     * 新增知识
     * @param addKnowVo vo类
     * @return String
     */
    @Override
    public String addKnow(AddKnowVo addKnowVo) {
        // 查询添加的知识是否存在
        ZhiShi zhiShi = zhiShiMapper.findOneByTxt(addKnowVo.getTxt());
        // 说明该知识不存在
        if (zhiShi == null){
            zhiShiMapper.addKnow(addKnowVo);
            return "插入成功";
        }
        return "插入失败";
    }

    @Override
    public PageInfo<ZhiShi> findListByTitle(String title, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ZhiShi> listByTitle = zhiShiMapper.findListByTitle(title);
        PageInfo pageInfo = new PageInfo(listByTitle);
        return pageInfo;
    }

    @Override
    public void DeleteById(Integer id) {
        zhiShiMapper.deleteById(id);
    }

    // 根据id查询知识
    @Override
    public UpdateKnowVo findOneById(Integer id) {
        ZhiShi zh = zhiShiMapper.findOneById(id);
        UpdateKnowVo updateKnowVo = new UpdateKnowVo();
        updateKnowVo.setId(zh.getId());
        updateKnowVo.setTitle(zh.getTitle());
        updateKnowVo.setTxt(zh.getTxt());
        return updateKnowVo;
    }

    @Override
    public Integer UpdateKnowById(UpdateKnowVo updateKnowVo) {
        return zhiShiMapper.updateKnowById(updateKnowVo);
    }
}
